﻿package com.bluediesel.utils.managers.scenemanager.commands {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class Command extends EventDispatcher {
		
		public var targets:Array = [];
		
		public function Command(obj:Object=null) {
			assembleTargets(obj);
			super();
		}
		
		protected function assembleTargets(obj:Object):void
		{
			if (obj != null){
				if (obj is Array){
					for each (var subObj:* in obj){
						if (subObj is Array)
							assembleTargets(subObj);
						else
							targets.push(subObj);
					}
				}else targets.push(obj);
			}
		}

		public function start(e:Event = null):void {
			executeAll();
		}
		
		public function executeAll():void {
			while (targets.length > 0) 
				execute(targets.pop());
			complete();
		}

		protected function countDownTargets():void
		{
			if (targets.length == 0)
				complete();
		}
		
		protected function execute(obj:Object = null):void {
			trace ("to be subclassed");
		}
		
		protected final function complete(e:Event = null):void {
			dispatchEvent(new Event(Event.COMPLETE));
			//trace ("COMMAND COMPLETE");
		}
	}
}